/**
* Volvo CareTrack Mobile Application Page definitions
* =============================
* 
* 
*/

registerNamespace('Volvo.CareTrack.page');

//Page renderer Class
Volvo.CareTrack.PageRendererClass = function() {
	this.defaultPage = "Start";
	this.templates = [$('#template_home')];
};

proto = Volvo.CareTrack.PageRendererClass.prototype;

proto.registPages = function() {
	for (var i = 0; i < this.templates.length; i++) {
		var template = this.templates[i];
		template.live('pagebeforecreate',function(event, ui){
//			alert('pagebeforecreate');
			Volvo.CareTrack.PageRenderer.renderPage(template);
	    });
		template.live('pageshow',function(event, ui){
			//alert('pageshow');			
			Volvo.CareTrack.PageRenderer.showPage(template);
	    });
	}
	console.log('registed all pages');
}


proto.renderPage = function(template) {
//	alert('render page');
	//template.css("display","block");
	var pageName = Volvo.CareTrack.Utilities.getFromStorage('currentPage');
	//Volvo.Caretrack.Utilities.removeFromStorage('currentPage');
//	alert("pageName="+pageName);
	var page = null;
	if (pageName !== undefined && pageName!=='' && pageName!==null) {
		page = new Volvo.CareTrack.page[pageName];
	}
	if (page == undefined || page==null) {
		page = new Volvo.CareTrack.page[this.defaultPage];
	}
	for (var div in page.components) {
		var divComponents = page.components[div];
		var divElement = $("#template_home > div[data-role='"+div+"']");
		divElement.text("");
		for (var i = 0; i < divComponents.length; i++) {
			var divComponent = divComponents[i];
			divComponent.render(divElement);
		}
	}
//	alert("done!");
}

proto.showPage = function(template){
//	alert('render page');
	//template.css("display","block");
	var pageName = Volvo.CareTrack.Utilities.getFromStorage('currentPage');
	//Volvo.Caretrack.Utilities.removeFromStorage('currentPage');
    //alert("pageName="+pageName);
	var page = null;
	if (pageName !== undefined && pageName!=='' && pageName!==null) {
		page = new Volvo.CareTrack.page[pageName];
	}
	for (var div in page.components) {
		var divComponents = page.components[div];
		var divElement = $("#template_home > div[data-role='"+div+"']");
		for (var i = 0; i < divComponents.length; i++) {
			var divComponent = divComponents[i];
			    if(divComponent.pageShow !== undefined){
			    	divComponent.pageShow(divElement);
			    }
				
		}
	}
//	alert("done!");
}

Volvo.CareTrack.PageRenderer = new Volvo.CareTrack.PageRendererClass();



//////////////Page Definitions   ////////////////

Volvo.CareTrack.page.Home = function(params) {
	
	this.components = {
			header : [new Volvo.CareTrack.UI.Components.HomeHeader({title: 'CareTrack Mobile'}), new Volvo.CareTrack.UI.Components.NavBar({})],	
			content : [new Volvo.CareTrack.UI.Components.HomeContent({})],
			footer : [new Volvo.CareTrack.UI.Components.HomeFooter({})]
	};

};

Volvo.CareTrack.page.Start = function(params) {
	
	this.components = {
			header : [new Volvo.CareTrack.UI.Components.Header({title: 'Start CareTrack Mobile'})],	
			content : [new Volvo.CareTrack.UI.Components.Start({})],
			footer : []
	};

};

Volvo.CareTrack.page.Login = function(params) {
	
	this.components = {
			header : [new Volvo.CareTrack.UI.Components.Header({title: 'Welcome to CareTrack Mobile'})],	
			content : [new Volvo.CareTrack.UI.Components.Login({})],
			footer : []
	};

};

Volvo.CareTrack.page.SearchMachine = function(params) {
	
	this.components = {
		header : [new Volvo.CareTrack.UI.Components.HomeHeader({title: 'CareTrack Mobile'}), new Volvo.CareTrack.UI.Components.NavBar({})],	
		content : [new Volvo.CareTrack.UI.Components.SearchMachine({})],
		footer : [new Volvo.CareTrack.UI.Components.HomeFooter({})]
	};

};

Volvo.CareTrack.page.MachinePosition = function(params) {
	
	this.components = {
		header : [new Volvo.CareTrack.UI.Components.HomeHeader({title: 'CareTrack Mobile'}), new Volvo.CareTrack.UI.Components.MachineNavBar({})],	
		content : [new Volvo.CareTrack.UI.Components.MachinePosition({})],
		footer : [new Volvo.CareTrack.UI.Components.HomeFooter({})]
	};

};

Volvo.CareTrack.page.MyPosition = function(params) {
	
	this.components = {
		header : [new Volvo.CareTrack.UI.Components.HomeHeader({title: 'CareTrack Mobile'}), new Volvo.CareTrack.UI.Components.MachineNavBar({})],	
		content : [new Volvo.CareTrack.UI.Components.MyPosition({})],
		footer : [new Volvo.CareTrack.UI.Components.HomeFooter({})]
	};

};

Volvo.CareTrack.page.Directions = function(params) {
	
	this.components = {
		header : [new Volvo.CareTrack.UI.Components.HomeHeader({title: 'CareTrack Mobile'}), new Volvo.CareTrack.UI.Components.MachineNavBar({})],	
		content : [new Volvo.CareTrack.UI.Components.Directions({})],
		footer : [new Volvo.CareTrack.UI.Components.HomeFooter({})]
	};

};
///////////////////////////////////////////////////
